package com.jhsw.roadsun.module.business.controller.admin.expressagegoods;

import com.jhsw.roadsun.framework.apilog.core.annotation.ApiAccessLog;
import com.jhsw.roadsun.framework.common.pojo.CommonResult;
import com.jhsw.roadsun.framework.common.pojo.PageParam;
import com.jhsw.roadsun.framework.common.pojo.PageResult;
import com.jhsw.roadsun.framework.common.util.object.BeanUtils;
import com.jhsw.roadsun.framework.excel.core.util.ExcelUtils;
import com.jhsw.roadsun.module.business.controller.admin.expressagegoods.vo.ExpressageGoodsPageReqVO;
import com.jhsw.roadsun.module.business.controller.admin.expressagegoods.vo.ExpressageGoodsRespVO;
import com.jhsw.roadsun.module.business.controller.app.expressage.vo.goods.AppExpressageGoodsSaveReqVO;
import com.jhsw.roadsun.module.business.dal.dataobject.expressagegoods.ExpressageGoodsDO;
import com.jhsw.roadsun.module.business.service.expressagegoods.ExpressageGoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.jhsw.roadsun.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.jhsw.roadsun.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 寄件货物信息")
@RestController
@RequestMapping("/business/expressage-goods")
@Validated
public class ExpressageGoodsController {

    @Resource
    private ExpressageGoodsService expressageGoodsService;

    @PostMapping("/create")
    @Operation(summary = "创建寄件货物信息")
    @PreAuthorize("@ss.hasPermission('business:expressage-goods:create')")
    public CommonResult<Long> createExpressageGoods(@Valid @RequestBody AppExpressageGoodsSaveReqVO createReqVO) {
        return success(expressageGoodsService.createExpressageGoods(createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除寄件货物信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:expressage-goods:delete')")
    public CommonResult<Boolean> deleteExpressageGoods(@RequestParam("id") Long id) {
        expressageGoodsService.deleteExpressageGoods(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得寄件货物信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:expressage-goods:query')")
    public CommonResult<ExpressageGoodsRespVO> getExpressageGoods(@RequestParam("id") Long id) {
        ExpressageGoodsDO expressageGoods = expressageGoodsService.getExpressageGoods(id);
        return success(BeanUtils.toBean(expressageGoods, ExpressageGoodsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得寄件货物信息分页")
    @PreAuthorize("@ss.hasPermission('business:expressage-goods:query')")
    public CommonResult<PageResult<ExpressageGoodsRespVO>> getExpressageGoodsPage(@Valid ExpressageGoodsPageReqVO pageReqVO) {
        PageResult<ExpressageGoodsDO> pageResult = expressageGoodsService.getExpressageGoodsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpressageGoodsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出寄件货物信息 Excel")
    @PreAuthorize("@ss.hasPermission('business:expressage-goods:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpressageGoodsExcel(@Valid ExpressageGoodsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpressageGoodsDO> list = expressageGoodsService.getExpressageGoodsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "寄件货物信息.xls", "数据", ExpressageGoodsRespVO.class,
                        BeanUtils.toBean(list, ExpressageGoodsRespVO.class));
    }

}